package com.coffeeloversupportlibrary.coffeelover.mercury.my.adapter;

import android.content.Context;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.appcompat.widget.AppCompatButton;

import com.bumptech.glide.Glide;
import com.coffeeloversupportlibrary.R;
import com.coffeeloversupportlibrary.coffeelover.mercury.entity.Shop;

import java.util.ArrayList;
import java.util.List;

/**
 * @author 盛守义
 * @createTime 2021/11/12 20:47
 * @projectName TurningCoffee
 * @className ShopAdapter.java
 * @description TODO
 */
public class ShopAdapter extends BaseAdapter {
    private Context context;
    private List<Shop.DataDTO.ListDTO> shops = new ArrayList<>();
    private int shop_item_id;

    private ImageView iv_bimgl;
    private TextView tv_name, tv_score;
    private AppCompatButton btn_exchange;
    private ShopClickListener mListener;

    public ShopAdapter(){
        super();
    }

    public ShopAdapter(List<Shop.DataDTO.ListDTO> shops, Context context, int shop_item_id, ShopClickListener listener){
        this.shops =  shops;
        this.context = context;
        this.shop_item_id = shop_item_id;
        this.mListener = listener;
    }

    @Override
    public int getCount() {
        return shops.size();
    }

    @Override
    public Object getItem(int i) {
        return shops.get(i);
    }

    @Override
    public long getItemId(int i) {
        return i;
    }

    @Override
    public View getView(int i, View view, ViewGroup viewGroup) {
        LayoutInflater layoutInflater = LayoutInflater.from(context);
        View layout = layoutInflater.inflate(shop_item_id, null);

        iv_bimgl = layout.findViewById(R.id.iv_bimglshop);
        tv_name = layout.findViewById(R.id.tv_bnamelshop);
        tv_score = layout.findViewById(R.id.tv_bscorelshop);
        btn_exchange = layout.findViewById(R.id.btn_exchangelshop);

        Glide.with(context).load(shops.get(i).getGoodsImg()).into(iv_bimgl);
        tv_score.setText(shops.get(i).getMallBeans().toString().trim());
        tv_name.setText(shops.get(i).getMallGoods().trim());
        btn_exchange.setTag(i);
        btn_exchange.setOnClickListener(mListener);
        return layout;
    }

    public abstract static class ShopClickListener implements View.OnClickListener {
        /**
         * 基类的onClick方法
         */
        @Override
        public void onClick(View v) {
            myOnClick((Integer) v.getTag(), v);
        }
        public abstract void myOnClick(int position, View v);
    }
}
